import { Component, Input, Output, EventEmitter, OnInit } from "@angular/core";
import { NzNotificationService } from "ng-zorro-antd";
import { Router, ActivatedRoute, ParamMap } from "@angular/router";
import { HttpServcie } from "../../../../service/http.service";
import { Config } from "../../../../service/config";
import { el } from "@angular/platform-browser/testing/src/browser_util";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";
import { MaintenanceRecord } from "../bean/maintenance-record.bean";
import { FileUploader, FileItem, ParsedResponseHeaders } from "ng2-file-upload";
import { Observable } from "rxjs/Rx";
import * as moment from "moment";

@Component({
  selector: "maintenance-detail",
  templateUrl: "maintenance-detail.html",
  styleUrls: ["maintenance-detail.scss"]
})
//电梯维修记录详情页
export class MaintenanceDetailPage implements OnInit {
  validateForm: FormGroup;
  isSpinning: boolean = false;
  functions: any = [];
  expandDataCache = {};
  @Input() inValue: any = [];
  data = [];
  @Output("outPut") outPut = new EventEmitter();
  config: any = Config;
  //接收参数
  maintenanceBean: MaintenanceRecord = new MaintenanceRecord();
  constructor(
    private fb: FormBuilder,
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private route: ActivatedRoute
  ) {}
  ngOnInit() {
    this.validateForm = this.fb.group({
      elevatorNum: [null, [Validators.required]],
      repairUserId: [null, [Validators.required]],
      replaceParts: [null, [Validators.required]],
      confirmUserId: [null, [Validators.required]],
      reportDate: [null, [Validators.required]],
      resolveDate: [null, [Validators.required]],
      faultDescription: [null, [Validators.required]],
      reason: [null, [Validators.required]],
      remarks: [null, [Validators.required]]
    });
    this.route.paramMap.subscribe(data => {
      //查询电梯的信息
      let id = data.get("id");
      this.httpServcie.queryByIdElevator(id).subscribe(datas => {
        if (datas.data) {
          this.maintenanceBean = datas.data;
          this.maintenanceBean.reportDate = this.transform(
            this.maintenanceBean.reportDate
          );
          this.maintenanceBean.resolveDate = this.transform(
            this.maintenanceBean.resolveDate
          );
        }
      });
    });
  }
  //交验
  getFormControl(name) {
    return this.validateForm.controls[name];
  }
  //点击返回按钮跳转主页面
  toBack() {
    window.history.back();
  }

  transform(value) {
    if (!value) {
      return null;
    }
    let values = value + "";
    let date = values.substring(0, 8);
    let time = values.substring(8, 14);
    let dates = moment(date).format("YYYY-MM-DD");
    let times =
      time.substring(0, 2) +
      ":" +
      time.substring(2, 4) +
      ":" +
      time.substring(4, 6);

    return dates + " " + times;
  }
}
